package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.ArrayList;
import javax.servlet.*;
import javax.servlet.http.*;
import models.DigitalClassQuestion;

public class AskQuestionController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("/askQuestion.jsp");
        dispatcher.forward(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        if (request.getParameter("question").length() < 20) {
            request.setAttribute("error", true);
        } else {
            Dbmanager dbManager = new Dbmanager();
            dbManager.openConnection();
            QueryManager queryManager = new QueryManager(dbManager);

            String question = new String();
            question = request.getParameter("question");
            if (question.length() <= 1000) {
                queryManager.addQuestion(question, 1);
                request.setAttribute("errorMessage", false);
                request.setAttribute("succesvol", true);
            } else {
                request.setAttribute("errorMessage", true);
                request.setAttribute("succesvol", false);
            }
            dbManager.closeConnection();
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("/askQuestion.jsp");
        dispatcher.forward(request, response);
    }
}
